﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using BizElements.Core.ProviderComponents;

namespace BizElements.Core
{
    /// <summary>
    /// Collection of <see cref="DbParameter"/> objects.
    /// </summary>
    /// <remarks>Simple parameter collection. SqlParameterCollection has to many constraints.</remarks>
    [Serializable]
    public sealed class DbParameterCollection : IEnumerable, IEnumerable<DbParameter>
    {
        #region Collection.

        List<DbParameter> list = new List<DbParameter>();

        /// <summary>
        /// Gets or sets the parameter at the specified postion.
        /// Setter throws exception is the given value is <b>null</b>.
        /// </summary>
        public DbParameter this[int index]
        {
            get { return this.list[index]; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value", Messages.DbParameterCollection_ParamMayNotBeNull);

                this.list[index] = value;
            }
        }

        /// <summary>
        /// Gets the parameter whith the specified name. Null if it doesn't exist.
        /// </summary>
        public DbParameter this[string name]
        {
            get
            {
                foreach (DbParameter par in this.list)
                {
                    if (par.ParameterName == name)
                        return par;
                }

                return null;
            }
        }

        /// <summary>
        /// Gets the total number of parameters in this collection.
        /// </summary>
        public int Count
        {
            get { return this.list.Count; }
        }

        /// <summary>
        /// Adds a parameter to the collection.
        /// </summary>
        /// <param name="par">Parameter. <b>Null</b> is not allowed.</param>
        public void Add(DbParameter par)
        {
            if (par == null)
                throw new ArgumentNullException("par", Messages.DbParameterCollection_ParamMayNotBeNull);

            this.list.Add(par);
        }

        /// <summary>
        /// Removes all parameters from the collection.
        /// </summary>
        public void Clear()
        {
            this.list.Clear();
        }

        #endregion

        #region IEnumerable.

        /// <summary>
        /// Gets an enumerator that can iterate through DbParameterCollection.
        /// </summary>
        public IEnumerator GetEnumerator()
        {
            return this.list.GetEnumerator();
        }

        #endregion

        #region IEnumerable<DbParameter> Members

        /// <summary>
        /// Gets an enumerator that can iterate through DbParameterCollection.
        /// </summary>
        IEnumerator<DbParameter> IEnumerable<DbParameter>.GetEnumerator()
        {
            return this.list.GetEnumerator();
        }

        #endregion
    }
}